﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.Text;

public partial class Search : Basic
{
    public string strRel = "";
    public string code = "";
    public string showCode = "";
    public string strRUrl = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        strRUrl = OTO.ToStr(Request.UrlReferrer);
        string str = Pub.LoadData(jsonUrl + "stock/HashStock2.ashx");
        List<SearchStock> listStock = new List<SearchStock>();
        listStock = JsonConvert.DeserializeObject<List<SearchStock>>(str);
        code = OTO.ToStr(Request["c"], "");
        int f = OTO.ToInt(Request["f"], 0);

        if (!string.IsNullOrEmpty(code))
        {
            showCode = "<span class=\"red\">" + code + "</span>";
            if (code.Length == 6)
                listStock = listStock.Where(o => o.C.Equals(code)).ToList();
            else
                listStock = listStock.Where(o => o.C.Contains(code)).ToList();

            if (f > 0)
                listStock = listStock.Where(o => o.F.Equals(f)).ToList();
        }
        else
        {
            listStock = new List<SearchStock>();
        }

        StringBuilder sb = new StringBuilder();
        if (listStock.Count == 1)
        {
            Response.Redirect(quoteUrl + RelUrl(listStock[0].F, listStock[0].C));
            Response.End();
        }
        else
        {
            if (listStock.Count == 0)
            {
                string fromUrl = Request.ServerVariables["HTTP_REFERER"];
                if (fromUrl == null) { fromUrl = "default.aspx"; }

                sb.AppendLine("<div align=\"center\" class=\"f14\">");
                sb.AppendFormat("没有找到与 <span class=\"red font14\">{0}</span> 有关的股票<br>请输入股票的代码/名称/拼音缩写，重新进行搜索<br><br>", showCode);
                //sb.AppendFormat("<span onclick=\"history.back();\">返回上一页</span>&nbsp;&nbsp;<a href=\"http://www.emoney.cn/\">返回益盟操盘手首页</a>", strRUrl);
                sb.AppendFormat("<a href=\"{0}\">返回上一页</a>&nbsp;&nbsp;<a href=\"http://www.emoney.cn/\">返回益盟操盘手首页</a>", fromUrl);
                sb.AppendLine("</div>");
            }
            else
            {
                List<SearchStock> listTmp = new List<SearchStock>();
                listTmp = listStock.Where(o => o.F == 1).ToList();
                if (listTmp.Count > 0)
                {
                    sb.AppendLine("<div class=\"main_content\">");
                    sb.AppendFormat("<span class=\"f14 fw fl\">&nbsp;&nbsp;沪深股市</span><span class=\"fr\">符合“{0}”的搜索结果：{1}条&nbsp;&nbsp;</span>", showCode, listTmp.Count);
                    sb.AppendLine("<div class=\"clr\"></div>");
                    sb.AppendLine("</div>");
                    sb.AppendLine("<ul class=\"search_content\">");
                    foreach (var m in listTmp)
                    {
                        sb.AppendFormat("<li><a href=\"{2}\">{0} {1}</a></li>", m.C.Replace(code, showCode), m.N, quoteUrl + RelUrl(1, m.C));
                    }
                    sb.AppendLine("</ul>");
                    sb.AppendLine("<div class=\"clr\">&nbsp;</div>	");
                }

                listTmp = listStock.Where(o => o.F == 2).ToList();
                if (listTmp.Count > 0)
                {
                    sb.AppendLine("<div class=\"main_content\">");
                    sb.AppendFormat("<span class=\"f14 fw fl\">&nbsp;&nbsp; 行业版块</span><span class=\"fr\">符合“{0}”的搜索结果：{1}条&nbsp;&nbsp;</span>", showCode, listTmp.Count);
                    sb.AppendLine("<div class=\"clr\"></div>");
                    sb.AppendLine("</div>");
                    sb.AppendLine("<ul class=\"search_content\">");
                    foreach (var m in listTmp)
                    {
                        sb.AppendFormat("<li><a href=\"{2}\">{0} {1}</a></li>", m.C.Replace(code,showCode), m.N, quoteUrl + RelUrl(2, m.C));
                    }
                    sb.AppendLine("</ul>");
                    sb.AppendLine("<div class=\"clr\"></div>");
                }

                listTmp = listStock.Where(o => o.F == 3).ToList();
                if (listTmp.Count > 0)
                {
                    sb.AppendLine("<div class=\"main_content\">");
                    sb.AppendFormat("<span class=\"f14 fw fl\">&nbsp;&nbsp; 指数</span><span class=\"fr\">符合“{0}”的搜索结果：{1}条&nbsp;&nbsp;</span>", showCode, listTmp.Count);
                    sb.AppendLine("<div class=\"clr\"></div>");
                    sb.AppendLine("</div>");
                    sb.AppendLine("<ul class=\"search_content\">");
                    foreach (var m in listTmp)
                    {
                        sb.AppendFormat("<li><a href=\"{2}\">{0} {1}</a></li>", m.C.Replace(code, showCode), m.N, quoteUrl + RelUrl(3, m.C));
                    }
                    sb.AppendLine("</ul>");
                    sb.AppendLine("<div class=\"clr\"></div>");
                }
            }
        }
        strRel = sb.ToString();
    }

    /// <summary>
    /// 返回对应的股票页面地址
    /// </summary>
    /// <param name="f">市场类型</param>
    /// <param name="c">代码</param>
    /// <returns></returns>
    protected string RelUrl(int f, string c)
    {
        string rel = "";
        switch (f)
        {
            case 1:
                rel = "GG" + c + ".shtml";
                //rel = "GG.aspx?code=" + c;
                break;
            case 2:
                rel = "BK" + c + ".shtml";
                //rel = "BK.aspx?code=" + c;
                break;
            case 3:
                rel = "DP" + c + ".shtml";
                //rel = "DP.aspx?code=" + c;
                break;
            default:
                break;
        }
        return rel;
    }


    protected class SearchStock
    {
        /// <summary>
        /// 代码
        /// </summary>
        public string C { get; set; }
        /// <summary>
        /// 股票名称
        /// </summary>
        public string N { get; set; }
        /// <summary>
        /// 标识 1=个股，2=板块，3=指数
        /// </summary>
        public int F { get; set; }
        /// <summary>
        /// 类型名称
        /// </summary>
        public string T { get; set; }
        /// <summary>
        /// 股票状态 1=开盘
        /// </summary>
        public string S { get; set; }
    }
}